package helexis.game.tyrian;

import helexis.game.tyrian.screen.HighScoresScreen;
import helexis.game.tyrian.screen.MenuScreen;
import helexis.game.tyrian.screen.OptionsScreen;
import helexis.game.tyrian.screen.SplashScreen;
import helexis.game.tyrian.screen.StartGameScreen;
import helexis.game.tyrian.service.LevelManager;
import helexis.game.tyrian.service.MusicManager;
import helexis.game.tyrian.service.PreferencesManager;
import helexis.game.tyrian.service.ProfileManager;
import helexis.game.tyrian.service.SoundManager;

import com.badlogic.gdx.Game;
import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.graphics.FPSLogger;

public class Tyrian extends Game {
	
	/**
	 * 로깅에 요긴하게 쓰일 상수 정의
	 */
	public static final String LOG = Tyrian.class.getSimpleName();
	
	/**
	 * 매 초마다 현재 FPS 를 로깅하는 libGDX 의 헬퍼 클래스 
	 */
	private FPSLogger fpsLogger;
	
	
	/**
	 * 현재 개발 모드 인지 나타내는 상수
	 */
	public static final boolean DEV_MODE = true;
	
	
	private SplashScreen splashScreen;
	private MenuScreen menuScreen;
	private StartGameScreen startGameScreen;
	private OptionsScreen optionsScreen;
	private HighScoresScreen highScoresScreen;
	
	
	private ProfileManager profileManager;
	
	private PreferencesManager preferencesManager;
	
	/**
	 * 사운드 관리자
	 */
	private SoundManager soundManager;
	
	/**
	 * 음악 관리자
	 */
	private MusicManager musicManager;
	
	
	/**
	 * 레벨 관리자
	 */
	private LevelManager levelManager;
	
	
	
	public Tyrian() {
		
		this.profileManager = new ProfileManager();
		this.preferencesManager = new PreferencesManager();
		
		this.soundManager = new SoundManager();
		this.musicManager = new MusicManager();
		
		this.levelManager = new LevelManager();
		
	}//end of constructor
	
	

	
	public SplashScreen getSplashScreen() {
		return splashScreen;
	}

	public MenuScreen getMenuScreen() {
		if (this.menuScreen == null) {
			this.menuScreen = new MenuScreen(this);
		}
		return menuScreen;
	}

	public StartGameScreen getStartGameScreen() {
		if (this.startGameScreen == null) {
			this.startGameScreen = new StartGameScreen(this);
		}
		return startGameScreen;
	}

	public OptionsScreen getOptionsScreen() {
		if (this.optionsScreen == null) {
			this.optionsScreen = new OptionsScreen(this);
		}
		return optionsScreen;
	}

	public HighScoresScreen getHighScoresScreen() {
		if (this.highScoresScreen == null) {
			this.highScoresScreen = new HighScoresScreen(this);
		}
		return highScoresScreen;
	}
	
	
	public ProfileManager getProfileManager() {
		return this.profileManager;
	}//end of getProfileManager()
	
	
	public PreferencesManager getPreferencesManager() {
		return this.preferencesManager;
	}//end of getPreferencesManager()
	
	
	public SoundManager getSoundManager() {
		return this.soundManager;
	}//end of getSoundManager()
	
	
	public MusicManager getMusicManager() {
		return this.musicManager;
	}//end of getMusicManager()
	
	
	public LevelManager getLevelManager() {
		return this.levelManager;
	}//end of getLevelManager()
	
	

	@Override
	public void create() {
		
		Gdx.app.log(LOG, "Creating game...");
		this.fpsLogger = new FPSLogger();
		
		/*
		 * 사운드 및 음악 관련 설정 로딩 후 적용
		 */
		this.soundManager.setEnabled(this.preferencesManager.isSoundEnabled());
		this.soundManager.setVolume(this.preferencesManager.getVolume());
		this.musicManager.setEnabled(this.preferencesManager.isMusicEnabled());
		this.musicManager.setVolume(this.preferencesManager.getVolume());
		
		this.splashScreen = new SplashScreen(this);
		
		setScreen(this.splashScreen);
		
		/*
		 * Profile 로딩
		 */
		this.profileManager.retrieveProfile();
		
	}//end of create()

	@Override
	public void resize(int width, int height) {
		
		super.resize(width, height);
		
		Gdx.app.log(LOG, "Resizing game to : " + width + " x " + height);
		
	}//end of resize()

	@Override
	public void render() {
		
		/*
		 * Tutorial 1.
		 * 
		 * RGB 녹색을 지정하여 화면을 클리어하는 코드
		 */
//		Gdx.gl.glClearColor(0f, 1f, 0f, 1f);
//		Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
		
		/*
		 * Tutorial 2.
		 */
		super.render();
		
		
		if (DEV_MODE) {
			
			/*
			 * 현재 FPS 를 로깅함
			 */
			this.fpsLogger.log();
			
		}//end if
		
	}//end of render()


	@Override
	public void pause() {
		
		Gdx.app.log(LOG, "Pausing game...");
		
		/*
		 * 현재 Profile 정보를 저장함
		 */
		this.profileManager.persist();
		
	}//end of pause()

	@Override
	public void resume() {
		
		Gdx.app.log(LOG, "Resuming game...");
		
	}//end of resume()
	
	@Override
	public void dispose() {
		
		Gdx.app.log(LOG, "Disposing game...");
		
		/*
		 * 프로파일 변경 분 저장
		 */
		this.profileManager.persist();
		
		/*
		 * 사운드 및 음악 종료
		 */
		this.soundManager.dispose();
		this.musicManager.dispose();
		
	}//end of dispose()
	
}//end of Tyrian.java
